package jdbc;

import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class JDBCDemo8 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您的用户名:");
        String username = sc.nextLine();
        System.out.println("请输入您的密码:");
        String password = sc.nextLine();
        System.out.println("请输入密码");
        String nickname = sc.nextLine();
        int age = sc.nextInt();
        System.out.println();
        try (Connection connection = DBUtil.getConnection();){
            String sql = "INSERT INTO user(username,password,nickname,age)"+
                    "VALUES(?,?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2, password);
            ps.setString(3,nickname);
            ps.setInt(4,age);

            int num = ps.executeUpdate();
            System.out.println(num>0?"注册成功":"注册失败");

        }catch (SQLException e){
            e.printStackTrace();
        }
    }
}
